﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Pr0ject0.Models;
using Pr0ject0.ViewModel;

namespace Pr0ject0.Controllers
{
    [Authorize(Roles = "Hiring Specialist")]
    public class HiringSpecialistController : Controller
    {
        JobApplicationDB applications = JobApplicationDB.GetInstance();
        HiringSpecialistSessions sessions = HiringSpecialistSessions.GetInstance();
        ReferenceDB references = new ReferenceDB();
        //
        // GET: /HiringSpecialist/

        public ActionResult Index()
        {
            return View(sessions.GetIndexModel());
        }

        public ActionResult Interviews()
        {
            return View(sessions.GetInterviewList(Membership.GetUser().UserName));
        }

        public ActionResult Details(int id)
        {
            return View(sessions.InterviewApplicant(
                Membership.GetUser().UserName, id));
        }

        [HttpPost]
        public ActionResult Details(int id, string str)
        {
           if (str == "accept")
           {
                applications.ChangeStatus(Membership.GetUser().UserName, id, 4);
            }
            else if (str == "reject")
            {
                applications.ChangeStatus(Membership.GetUser().UserName, id, 1);
            }                
            return RedirectToAction("Interviews");
        }

        public ActionResult Background()
        {
            return View(sessions.GetBackgroundList(Membership.GetUser().UserName));
        }

        public ActionResult BeginReferenceCheck(int id)
        {
            sessions.BeginReferenceCheck(Membership.GetUser().UserName, id);
            return RedirectToAction("Reference");
        }

        public ActionResult Reference()
        {
            ReferenceModel model;
            string username = Membership.GetUser().UserName;
            model = sessions.NextReference(username);

            if (model == null)
            {
                return RedirectToAction("Summary");
            }

            return View(model);
        }
        
        [HttpPost]
        public ActionResult Reference(string id, string str)
        {
            references.ChangeReferenceStatus(Convert.ToInt32(id), Convert.ToBoolean(str));
            return RedirectToAction("Reference");
        }

        public ActionResult Summary()
        {
            ViewBag.applicationId = sessions.CurrentApplication(Membership.GetUser().UserName).ID;
            return View(sessions.GetReferenceList(Membership.GetUser().UserName));
        }

        [HttpPost]
        public ActionResult Summary(int id, string str)
        {
            if (sessions.CurrentApplication(Membership.GetUser().UserName).ID == id)
            {
                if (str == "accept")
                {
                    return RedirectToAction("BackgroundCheck");
                }
                else if (str == "reject")
                {
                    applications.ChangeStatus(Membership.GetUser().UserName, id, 1);
                    sessions.ClearState(Membership.GetUser().UserName);
                    RedirectToAction("Background");
                }
                else
                {
                    sessions.ClearState(Membership.GetUser().UserName);
                    RedirectToAction("Background");
                }
            }
            return RedirectToAction("Index");
        }

        public ActionResult BackgroundCheck()
        {
            ViewBag.applicationId = sessions.CurrentApplication(Membership.GetUser().UserName).ID;
            return View();
        }

        [HttpPost]
        public ActionResult BackgroundCheck(int id, string str)
        {
            if (sessions.CurrentApplication(Membership.GetUser().UserName).ID == id)
            {
                if (str == "accept")
                {
                    applications.ChangeStatus(Membership.GetUser().UserName, id, 6);
                    sessions.ClearState(Membership.GetUser().UserName);
                    return RedirectToAction("Background");
                }
                else if (str == "reject")
                {
                    applications.ChangeStatus(Membership.GetUser().UserName, id, 1);
                    sessions.ClearState(Membership.GetUser().UserName);
                    RedirectToAction("Background");
                }
                else
                {
                    sessions.ClearState(Membership.GetUser().UserName);
                    RedirectToAction("Background");
                }
            }
            return RedirectToAction("Index");
        }
    }
}
